<template>
  <div v-show="$route.name == 'goods-save'">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/goods-list' }">商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>商品新增</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <div style="margin-bottom: 20px;">商品新增</div>
      <el-form :model="goods" :rules="rules" ref="goods" label-width="100px" class="demo-saveForm">
        <el-row>
          <el-col :span="10">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="goods.name" placeholder="请输入商品名称" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="品类" prop="process">
              <el-radio-group v-model="goods.process">
                <el-radio :label="0">原料</el-radio>
                <el-radio :label="1">成品</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="分类" prop="category_id">
              <el-select v-model="goods.category_id" popper-class="dropdownbox" filterable style="width:200px" placeholder="请选择品类">
                <el-option v-for="item in categoryList" :label="item.category_name"
                  :value="item.id" :key="item.id">
                  <span style="float: left">{{ item.category_name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="goods.process ? '参考成本' : '默认进货价'" prop="price_in">
              <el-input-number controls-position="right" :precision="2" :step="1" :min="0" v-model="goods.price_in" placeholder="请输入默认进货价" style="width:200px"></el-input-number>
            </el-form-item>
            <el-form-item label="进货单位" prop="unit_in">
              <el-input v-model="goods.unit_in" placeholder="请输入进货单位，如：箱、斤、个" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="规格" prop="specs">
              <el-input v-model="goods.specs" placeholder="请输入规格" style="width:200px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item label="是否可销售" prop="sellable">
              <el-radio-group v-model="goods.sellable">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="转换比例" prop="conversion">
              <el-input-number controls-position="right" :precision="2" :step="1" :min="0.01" v-model="goods.conversion" placeholder="请输入转换比例" style="width:200px"></el-input-number>
              <el-alert title="请输入转换比例，如：10，代表1个进货单位=10个销售单位" type="warning" show-icon style="width: 500px;height: 30px;margin-top: 5px;margin-bottom: 28px;"></el-alert>
            </el-form-item>
            <el-form-item label="销售名称" prop="sale_name">
              <el-input v-model="goods.sale_name" placeholder="请输入销售名称" style="width:200px"></el-input>
            </el-form-item>
            <el-form-item label="销售售价" prop="price">
              <el-input-number controls-position="right" :precision="2" :step="1" :min="0" v-model="goods.price" placeholder="请输入销售售价" style="width:200px"></el-input-number>
            </el-form-item>
            <el-form-item label="销售单位" prop="unit">
              <el-input v-model="goods.unit" placeholder="请输入销售单位，如：箱、斤、个" style="width:200px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button type="primary" @click="handleSubmit">提交</el-button>
            <el-button  @click="back" style="margin-left: 5px">返回</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goods: {
        name: '',
        category_id: '',
        process: 1,
        price_in: 0,
        unit_in: '',
        specs: '',
        conversion: 1,
        sale_name: '',
        price: 0,
        unit: '',
        sellable: 1
      },
      categoryList: [],
      rules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        process: [
          { required: true, message: '请选择品类', trigger: 'change' }
        ],
        category_id: [
          { required: true, type: 'number', message: '请选择分类', trigger: 'change' }
        ],
        price_in: [
          { required: true, message: '请输入默认进货价', trigger: 'blur' }
        ],
        unit: [
          { required: true, message: '请输入进货单位', trigger: 'blur' }
        ],
        conversion: [
          { required: true, message: '请输入转换比例', trigger: 'blur' }
        ],
        sale_name: [
          { required: true, message: '请输入销售名', trigger: 'blur' }
        ],
        sellable: [
          { required: true, message: '请选择是否可以销售', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入销售售价', trigger: 'blur' }
        ],
        unit_in: [
          { required: true, message: '请输入销售单位', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getCategoryList()
  },
  methods: {
    // 获取分类
    async getCategoryList() {
      const { data: result } = await this.$http.get('/category')
      if (result.status !== 200) return this.$message.error(result.msg)
      this.categoryList = result.data
    },
    back () {
      this.$router.push({'name': 'goods-list'})
    },
    async handleSubmit() {
      this.$refs.goods.validate(async (valid) => {
        if (!valid) return
        let data = {
          name: this.goods.name,
          process: this.goods.process,
          category_id: this.goods.category_id,
          price_in: this.goods.price_in,
          unit_in: this.goods.unit_in,
          specs: this.goods.specs ? this.goods.specs : '',
          sellable: this.goods.sellable,
          conversion: this.goods.conversion,
          sale_name: this.goods.sale_name,
          price: this.goods.price,
          unit: this.goods.unit
        }
        // 可以发起新增入库单的网络请求
        const { data: result } = await this.$http.post('/goods', data)
        if (result.status !== 200) {
          return this.$message.error(result.msg)
        }
        this.$message.success('保存成功！')
        this.$router.push({ path: '/goods-list' })

      })
    }
  }
}
</script>
<style lang="less">
  .marginLeft15 {
    margin-left: 15px
  }
</style>